import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SearchableConfigurable;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * Created by zuoshuai on 2017/3/22.
 */
public class FormatConfigtion implements SearchableConfigurable {
    private TestForm testForm;
    private FormatSetting formatSetting = FormatSetting.getInstance();

    @NotNull
    @Override
    public String getId() {
        return "DocFormat";
    }

    @Nls
    @Override
    public String getDisplayName() {
        return this.getId();
    }

    @Nullable
    @Override
    public String getHelpTopic() {
        return this.getId();
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        if (this.testForm == null) {
            testForm = new TestForm();
        }
        return this.testForm.mainPanel;
    }

    @Override
    public boolean isModified() {
        return !this.formatSetting.getGetFormat().equals(this.testForm.getFormatTextArea.getText()) ||
                !this.formatSetting.getSetFormat().equals(this.testForm.setFormatTextArea.getText());
    }

    @Override
    public void apply() throws ConfigurationException {
        this.formatSetting.setGetFormat(this.testForm.getFormatTextArea.getText());
        this.formatSetting.setSetFormat(this.testForm.setFormatTextArea.getText());
    }

    @Override
    public void reset() {
        this.testForm.getFormatTextArea.setText(this.formatSetting.getGetFormat());
        this.testForm.setFormatTextArea.setText(this.formatSetting.getSetFormat());
    }
}
